package util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public  class Utils {
	
	public static char[] pRNumbers= {102,114,101,101,100,111,109,77,101,115,115,97,103,101};
	
	public static String[] readConfigurationFileForDB(){
		//trova nomeUtente,password e host per i db axdbv4 dal file C:\config\db.properties
		
				//ritorna un array di stringhe in cui 
				//elemento 0 : host
				//elemento 1 : user
				//elemento 2 : password
		
		
		//File configFile=new File(File.separator+"Users"+File.separator+"andrea"+File.separator+"" +
		//		"Desktop"+File.separator+"db.properties");
		
		String[] parameters=new String[3];
		File configFile=new File("C:"+File.separator+"config"+File.separator+"db.properties");
		
		
		
		
		String userLineInit="axdbUser=";
		String passLineInit="axdbPwd=";
		String axdbUrlLineInit="axdbUrl=";
		
		String axdbUrl="";
		String user="";
		String password="";
		
		try {
			System.out.println("Sto per leggere il file: "+configFile.getCanonicalPath());
			BufferedReader reader = new BufferedReader(new FileReader(configFile));
			String currentLine=reader.readLine();
			while (currentLine!=null){
				String trimmedLine = currentLine.trim();
				if(trimmedLine.contains(axdbUrlLineInit)){
					axdbUrl=currentLine.replace(axdbUrlLineInit, "");
					
				}
				if(trimmedLine.contains(userLineInit)){
					user=currentLine.replace(userLineInit, "");
				}
				if(trimmedLine.contains(passLineInit)){
					password=currentLine.replace(passLineInit, "");
				}
				currentLine=reader.readLine();
			}
			
			
			parameters[0]=axdbUrl;
			parameters[1]=user;
			parameters[2]=password;
			 
			
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			return null;
		//	e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			return null;
			//e.printStackTrace();
		}
		
		return parameters;
		
		
	}
	
	
	
	public static void printProgBar(int percent){
	    StringBuilder bar = new StringBuilder("[");

	    for(int i = 0; i < 50; i++){
	        if( i < (percent/2)){
	            bar.append("=");
	        }else if( i == (percent/2)){
	            bar.append(">");
	        }else{
	            bar.append(" ");
	        }
	    }

	    bar.append("]   " + percent + "%     ");
	    System.out.print("\r" + bar.toString());
	}

	public static void main(String[] args) {
		System.out.print(Utils.readConfigurationFileForDB()[0]);
	}
}
